<%--
  - Author		:	Jaewon Choi
  - Date			:
  - Description	:	
  --%>
<%@ page language="java" %>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ page pageEncoding="utf-8"%>
<%@ include file="/WEB-INF/pages/includes/taglibs.jspf" %>
<%@ include file="/WEB-INF/pages/includes/variables.jspf" %>
<%@ include file="/WEB-INF/pages/includes/doctype.jspf" %>
<html>
	<head>
<%@ include file="/WEB-INF/pages/includes/metatags.jspf" %>
		<title><fmt:message key="pages.title" /></title>
		<link rel="stylesheet" type="text/css" href="<c:url value="${dirStyles}/common.css" />" />

		<script type="text/javascript" src="<c:url value="${dirScripts}/jquery-1.7.2.min.js" />"></script>
		<script type="text/javascript">
			$(function(){
			});
			
			function chkDuplicationId(){
				var idx = "0";
				var id = document.frm.id.value;
				if(document.frm.idx.value){
					idx = document.frm.idx.value;
				}
				/*
				$.ajax({
					type: "POST",
					url: "<c:url value="/content/chkContNm" />",
					dataType: "json",
					async: false,
					data: {
								"idx" : idx,
								"chkId" : chkId
							},
					success: function(data){
						if(data.resultCode == "200"){
							chkDuplicationFlag = data.result;
							if(eval(chkDuplicationFlag)){
								$(document.frm.id).css("background", "#ADC900");
							}else{
								$(document.frm.id).css("background", "#F2685E");
								alert("아이디가 중복 됩니다.");
							}
						}else{
							alert(data.errMsg);
						}
					},
					beforeSend: function(){
					},
					error: function(jqXHR, textStatus, errorThrown){
						//alert(jqXHR.responseText); //에러 페이지 내용
						alert("오류가 발생하였습니다.\n관리자에게 문의 바랍니다.");
						throw errorThrown;
					},
					complete: function(){
					}
				 });
				*/
			
				$.ajax({
					type: "POST",
					url: "<c:url value="/users/chkDupl" />",
					dataType: "json",
					async: false,
					data: {
								"idx" : idx,
								"chkId" : id
							},
					success: function(data){
							chkDuplicationFlag = data;							
							if(!eval(chkDuplicationFlag)){
								$(document.frm.id).css("background", "#ADC900");
							}else{
								$(document.frm.id).css("background", "#F2685E");
								alert("아이디가 중복 됩니다.");
							}
					},
					beforeSend: function(){
					},
					error: function(jqXHR, textStatus, errorThrown){
						//alert(jqXHR.responseText); //에러 페이지 내용
						alert("오류가 발생하였습니다.\n관리자에게 문의 바랍니다.");
						throw errorThrown;
					},
					complete: function(){
					}
				 });
				return eval(!chkDuplicationFlag);
			}

			function validation(){
				<c:if test="${param.fAct eq 'view'}">
					return true;
				</c:if>
				try{					
					if(! chkDuplicationId()){
						return false;
					}				
					with(document.frm){
						
						if(id.value.replace(/\s/g, '') == ""){
							alert("[아이디]은 필수 입력 항목입니다.");
							id.focus();
							return false;
						}		
						
						if(id.value.length < 4){
							alert("[아이디]는 4자리 이상 15자리 이하로 입력하셔야 합니다.");
							id.focus();
							return false;
						}
						
						if(id.value.length > 15){
							alert("[아이디]는 4자리 이상 15자리 이하로 입력하셔야 합니다.");
							id.focus();
							return false;
						}
						
						if(password.value.replace(/\s/g, '') == ""){
							alert("[패스워드]는 필수 입력 항목입니다.");
							password.focus();
							return false;
						}
						
						if(password.value.length < 8){
							alert("[아이디]는 8자리 이상 이어야 합니다.");
							id.focus();
							return false;
						}
						
						if(password.value != passwordChk.value){
							alert("[패스워드]와 [패스워드 확인] 값이 다릅니다.");
							password.focus();
							return false;
						}
		
						if($(".cIdx option:selected").val() == ""){
							alert("[회원사]를 선택하여주십시요.");
							$(".cIdx").focus();
							return false;
						}
						
						if($(".cIdx option:selected").val() == "1"){
							authority.value = "ROLE_ADMIN";	
						} else {
							authority.value = "ROLE_USER";	
						}
						
					}
				}catch(err){
					return false;	
				}
				
				return confirm(confirmMsg);
			}
			var confirmMsgs = ["등록 하시겠습니까?", "수정 하시겠습니까?", "삭제 하시겠습니까?"];
			var confirmMsg = "";
		</script>
	</head>
	<body>
		<div class="bodyWrap">
<%@ include file="/WEB-INF/pages/includes/header.jsp" %>
<%@ include file="/WEB-INF/pages/includes/left.jsp" %>
			<div class="contWrap">
				<div class="menuTitle">
					회원사별이용자관리 - 이용자<c:choose>
						<c:when test="${param.fAct eq 'view'}">상세보기</c:when>
						<c:when test="${not empty users}">수정</c:when>
						<c:otherwise>등록</c:otherwise>
					</c:choose>
				</div>
				<form id="frm" name="frm" method="post" enctype="multipart/form-data" action="<c:url value="/users/submit.do" />" onsubmit="return validation();">
					<input type="hidden" name="maxnum" value="${param.maxnum}" />
					<input type="hidden" name="page" value="${param.page}" />
					<input type="hidden" name="searchId" value="${param.searchId}" />
					<input type="hidden" name="searchName" value="${param.searchName}" />
					<input type="hidden" name="idx" value="${users.idx}">
					<input type="hidden" name="authority" value="${users.authority}" />
					<table class="frmTbl" border="0">
						<colgroup>
							<col width="176px;" />
							<col width="*" />
						</colgroup>
						<tbody>
							<tr>
								<th>회원사</th>
								<td>
									<c:choose>
										<c:when test="${param.fAct eq 'view'}">
											<c:forEach items="${customerList}" var="customer">
												<c:if test="${customer.idx eq users.cIdx}">
													${customer.name}
												</c:if>
											</c:forEach>
										</c:when>
										<c:when test="${not empty users}">
											<c:forEach items="${customerList}" var="customer">
												<c:if test="${customer.idx eq users.cIdx}">
													${customer.name}
													<input type="hidden" name="cIdx" value="${customer.idx}" title="회원사명입니다." />
												</c:if>
											</c:forEach>
										</c:when>
										<c:otherwise>
											<select class="cIdx" name="cIdx" title="회원사명입니다.">
												<c:forEach items="${customerList}" var="customer">
													<option value="${customer.idx}" <c:if test="${customer.idx eq users.cIdx}"> selected="selected" </c:if>>${customer.name}</option>
												</c:forEach>
											</select>
										</c:otherwise>
									</c:choose>
								</td>
							</tr>
							<tr>
								<th>ID</th>
								<td>
									<c:choose>
										<c:when test="${param.fAct eq 'view'}">
											${users.id}
										</c:when>
										<c:when test="${not empty users}">
											${users.id}
											<input type="hidden" name="id" value="${users.id}" maxlength="15" title="이용자 ID입니다." />
										</c:when>
										<c:otherwise>
											<input type="text" name="id" value="${users.id}" maxlength="15" title="이용자 ID입니다." /> 4자리이상 15자리 이하(특수문자 허용 및 영대소문자 구분함)
										</c:otherwise>
									</c:choose>
								</td>
							</tr>
							<c:if test="${param.fAct ne 'view'}">
								<tr>
									<th>Password</th>
									<td>
										<input type="password" name="password" value="" maxlength="15" title="비밀번호 입니다." /> 8자리 이상 15자리 이하(특수문자 허용 및 영대소문자 구분함)
									</td>
								</tr>
								<tr>
									<th>Password 확인</th>
									<td>
										<input type="password" name="passwordChk" value="" maxlength="15" title="비밀번호 입니다." />
									</td>
								</tr>
							</c:if>	
							<tr>
								<th>이름</th>
								<td>
									<c:choose>
										<c:when test="${param.fAct eq 'view'}">
											${users.name}
										</c:when>
										<c:otherwise>
											<input type="text" name="name" value="${users.name}" maxlength="15" title="이름 입니다." />
										</c:otherwise>
									</c:choose>
								</td>
							</tr>
							<!-- <tr>
								<th>권한</th>
								<td>
									 <c:choose>
										<c:when test="${param.fAct eq 'view'}">
											<c:choose>
												<c:when test="${users.authority eq 'ROLE_ADMIN'}">	
													최고 관리자
												</c:when>
												<c:otherwise>
													이용자
												</c:otherwise>
											</c:choose>
										</c:when>
										<c:otherwise>
											<input type="text" name="authority" value="${users.authority}" maxlength="15" title="권한 입니다." />
										</c:otherwise>
									</c:choose>
								</td>
							</tr> -->							
							<tr>
								<th>사용 여부</th>
								<td>
									<c:choose>
										<c:when test="${param.fAct eq 'view'}">
											<c:choose>
												<c:when test="${users.enabled eq '1'}">	
													사용
												</c:when>
												<c:otherwise>
													미사용
												</c:otherwise>
											</c:choose>
										</c:when>
										<c:otherwise>
											<select name="enabled" title="사용 여부 입니다.">
												<option value="1" <c:if test="${users.enabled eq '1'}">selected="selected"</c:if>>사용</option>
												<option value="0" <c:if test="${users.enabled eq '0'}">selected="selected"</c:if>>미사용</option>
											</select>
										</c:otherwise>
									</c:choose>
								</td>
							</tr>
						</tbody>
					</table>
					<c:choose>
						<c:when test="${empty users}">
							<input type="button" class="btn" value="취소" onclick="javascript:document.listFrm.submit();" />
							<input type="submit" class="btn" value="등록" onclick="javasciprt:confirmMsg=confirmMsgs[0];"/>
						</c:when>
						<c:when test="${param.fAct eq 'view'}">
							<input type="button" class="btn" value="목록" onclick="javascript:document.listFrm.submit();" />
							<input type="submit" class="btn" value="수정" onclick="javasciprt:document.frm.action='<c:url value="/users/form.do" />';" />
						</c:when>
						<c:otherwise>
							<input type="button" class="btn" value="취소" onclick="javascript:document.listFrm.submit();" />
							<input type="submit" class="btn" value="수정" onclick="javasciprt:confirmMsg=confirmMsgs[1];"/>
							<input type="button" class="btn" value="삭제" onclick="javascript:if(confirm(confirmMsgs[2])){document.removeFrm.submit();}" />
						</c:otherwise>
					</c:choose>
				</form>
				<form name="listFrm" action="<c:url value="/users/list.do" />" method="post" style="display: none;">
					<input type="hidden" name="maxnum" value="${param.maxnum}" />
					<input type="hidden" name="page" value="${param.page}" />
					<input type="hidden" name="id" value="${param.searchId}" />
					<input type="hidden" name="name" value="${param.searchName}" />
				</form>
				
				<form name="removeFrm" action="<c:url value="/users/remove.do" />" method="post" style="display: none;">
					<input type="hidden" name="fAct" value="remove">
					<input type="hidden" name="idx" value="${users.idx}">
					<input type="hidden" name="maxnum" value="${param.maxnum}" />
					<input type="hidden" name="page" value="${param.page}" />
					<input type="hidden" name="searchId" value="${param.searchId}" />
					<input type="hidden" name="searchName" value="${param.searchName}" />
				</form>
			</div>
<%@ include file="/WEB-INF/pages/includes/footer.jsp" %>
		</div>
	</body>
</html> 